package com.example;

import com.example.bean.Klass;
import com.example.bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lv
 * @date 2021/4/18 16:00
 */
@Configuration
@EnableConfigurationProperties(AutoConfigurationProperties.class)
@ConditionalOnClass({Student.class, Klass.class})
@ConditionalOnProperty(prefix = "com.example", value = "enabled", matchIfMissing = true)
public class AutoConfiguration {

    @Autowired
    private AutoConfigurationProperties autoConfigurationProperties;
    private Student student = new Student();
    @Bean
    @ConditionalOnMissingBean(Student.class)
    public Student studentConfiguration() {
        student.setId(autoConfigurationProperties.getId());
        student.setName(autoConfigurationProperties.getName());
        return student;
    }
    @Bean
    @ConditionalOnMissingBean(Klass.class)
    public Klass klassConfiguration() {
        Klass klass = new Klass();
        List<Student> students = new ArrayList<Student>();
        students.add(student);
        klass.setStudents(students);
        return klass;
    }
}
